from utils.spark.gpt import SparkApi
from ws.config.constants import xunfei
class GPT:
    text =[]

    def getText(self, role, content):
        jsoncon = {}
        jsoncon["role"] = role
        jsoncon["content"] = content
        self.text.append(jsoncon)
        return self.text

    def getlength(self, text):
        length = 0
        for content in text:
            temp = content["content"]
            leng = len(temp)
            length += leng
        return length

    def checklen(self, text):
        while (self.getlength(text) > 8000):
            del text[0]
        return text

    def main(self,input):
        question = self.checklen(self.getText("user", input))
        print(question)
        SparkApi.answer = ""
        print("小天:", end="")
        SparkApi.main(xunfei.appid.value,
                      xunfei.api_key.value,
                      xunfei.api_secret.value,
                      xunfei.Spark_url1.value,
                      xunfei.domain1.value,
                      question)
        return self.getText("assistant", SparkApi.answer)